<?php

class ProfileDocumentTypeForm extends BaseForm
{

    private $profileDocumentTypeConfigurationService;

    public function getProfileDocumentTypeConfigurationService()
    {

        if (!($this->profileDocumentTypeConfigurationService instanceof ProfileDocumentTypeConfigurationService)) {
            $this->profileDocumentTypeConfigurationService = new ProfileDocumentTypeConfigurationService();
        }

        return $this->profileDocumentTypeConfigurationService;
    }

    public function setProfileDocumentTypeConfigurationService($profileDocumentTypeConfigurationService)
    {
        $this->profileDocumentTypeConfigurationService = $profileDocumentTypeConfigurationService;
    }

    public function configure()
    {

        $this->setWidgets(array(
            'id' => new sfWidgetFormInputHidden(),
            'code' => new sfWidgetFormInputText(),
            'name' => new sfWidgetFormInputText(),
            'description' => new sfWidgetFormTextArea()
        ));

        $this->setValidators(array(
            'id' => new sfValidatorNumber(array('required' => false)),
            'code' => new sfValidatorString(array('required' => true, 'max_length' => 10, 'trim' => true)),
            'name' => new sfValidatorString(array('required' => true, 'max_length' => 100, 'trim' => true)),
            'description' => new sfValidatorString(array('required' => false, 'max_length' => 600, 'trim' => true))
        ));

        $this->widgetSchema->setNameFormat('profileDocumentType[%s]');

        $this->setDefault('id', '');
    }

    public function save()
    {

        $id = $this->getValue('id');

        if (empty($id)) {
            $profileDocumentType = new ProfileDocumentType();
            $message = array('messageType' => 'success', 'message' => __(TopLevelMessages::SAVE_SUCCESS));
        } else {
            $profileDocumentType = $this->getProfileDocumentTypeConfigurationService()->getProfileDocumentType($id);
            $message = array('messageType' => 'success', 'message' => __(TopLevelMessages::UPDATE_SUCCESS));
        }

        $profileDocumentType->setCode($this->getValue('code'));
        $profileDocumentType->setName($this->getValue('name'));
        $profileDocumentType->setDescription($this->getValue('description'));
        $this->getProfileDocumentTypeConfigurationService()->saveProfileDocumentType($profileDocumentType);

        return $message;

    }

}
